package com.logistic.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.logistic.dao.AddressDao;
import com.logistic.dao.HibernateSessionFactory;
import com.logistic.domain.PickupAddressCN;


public class AddressDaoImpl extends HibernateDaoSupport implements AddressDao {

	/**
	 * 获取所有记录
	 * 
	 * @param sort
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getAll() {
		List list = null;
		try{
			Session s = HibernateSessionFactory.getSession();
			String hql = "from PickupAddressCN addr order by id asc";
			list = s.createQuery(hql).list();
			if (list.isEmpty()) {
				return null;
			}
		}catch (HibernateException e)
        {
           e.printStackTrace();
        } finally
        {
        	HibernateSessionFactory.closeSession();
        }
			
		return list;
	}
	
	public List getAllByUid(String uId){
		List list = null;
		try
		{
			Session s = HibernateSessionFactory.getSession();
			String hql = "from PickupAddressCN addr where Uid ='" + uId+"'";
			list = s.createQuery(hql).list();
			if (list.isEmpty()) {
				return null;
			}
		}catch(HibernateException e)
		{
			e.printStackTrace();
		}finally
		{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	//根据id找到这个对象
	public PickupAddressCN queryAddress(String addrId){
		List list = null;
		try
		{
			Session s = HibernateSessionFactory.getSession();
			String hql = "from PickupAddressCN addr where addr.PUaddrId ='" + addrId+"'";
			list = s.createQuery(hql).list();
			if (list.isEmpty()) {
				return null;
			}
		}catch(HibernateException e)
		{
			e.printStackTrace();
		}finally
		{
			HibernateSessionFactory.closeSession();
		}
		return (PickupAddressCN)list.get(0);
	}

	/**
	 *保存
	 */
	@SuppressWarnings("finally")
	public int save(PickupAddressCN addr) {

		Session s = HibernateSessionFactory.getSession();
		Transaction t = s.beginTransaction();
		
		s.saveOrUpdate(addr);
		try {
			t.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
			return 1;
		}
	}

	/**
	 * 修改
	 */
	public int modify(PickupAddressCN addr) {

		Session s = HibernateSessionFactory.getSession();
		Transaction t = s.beginTransaction();
		Query q = s.createQuery("update PickupAddressCN addr set addr.Uid=?, addr.PUname=?, " 
				+ "addr.PUphoneNum=?, addr.PUsparePhoneNum=?, addr.PUarea=?, addr.PUaddress=?,addr.PUzip=?, "
				+ "addr.PUnote=?"
				+ " where addr.PUaddrId=?");
		q.setParameter(0, addr.getUid());
		q.setParameter(1, addr.getPUname());
		q.setParameter(2, addr.getPUphoneNum());
		q.setParameter(3, addr.getPUsparePhoneNum());
		q.setParameter(4, addr.getPUarea());
		q.setParameter(5, addr.getPUaddress());
		q.setParameter(6, addr.getPUzip());
		q.setParameter(7, addr.getPUnote());
		q.setParameter(8, addr.getPUaddrId());
		
		try{
			q.executeUpdate();
//		s.saveOrUpdate(record);
			t.commit();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}

		return 1;
	}
	
	/**
	 * 根据Ids删除Record
	 * 
	 * @return
	 */
	public void delete(String id) {
		// this.getSession().delete(record);
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction tran = s.beginTransaction();
			Query q = s.createQuery("delete from PickupAddressCN where PUaddrId=?");
			q.setParameter(0, id);
			q.executeUpdate();
			tran.commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally
		{
			HibernateSessionFactory.closeSession();
		}
		
	}
}
